package com.code.leetcode.offer;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/8/1 9:11
 */
public class The19_dp {
    public boolean isMatch(String s, String p) {
        int m = s.length();
        int n = p.length();
        boolean[][] dp = new boolean[m + 1][n + 1];

        for (int i = 0; i < m + 1; i++) {
            for (int j = 0; j < n + 1; j++) {
                if (j == 0) {
                    dp[i][j] = i == 0;
                } else {
                    if (p.charAt(j - 1) != '*') {
                        if ((i > 0 &&(p.charAt(j - 1) == s.charAt(i - 1) || p.charAt(j - 1) == '.')))
                            dp[i][j] = dp[i - 1][j - 1];
                    } else {
                        if (j >= 2) {
                            dp[i][j] |= dp[i][j - 2];
                        }
                        if (i >= 1 && j >= 2 && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.')) {
                            dp[i][j] |= dp[i - 1][j];
                        }
                    }
                }
            }
        }
        return dp[m][n];
    }

    public static void main(String[] args) {
        The19_dp the19_dp = new The19_dp();
        System.out.println(the19_dp.isMatch("aa", "a*"));
    }

}
